﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAFB02 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public YCAFB02Data m_Data, m_DataX;
        public DataSet dsParamaters;
        public YCAFB02Report m_rpt;
        bool IsSupplierLoded = false;
        public string PrnType = "EXCEL";
        BindingListCollectionView cvMaster
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgMaster.ItemsSource);
            }
        } 
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        public SETINTEXDB SetEvn
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEvn");
            }
        }  
        #region=====vwMaster=======
        CollectionViewSource vwMaster
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMaster");
            }
        }
        DataRow dwMaster
        {
            get
            {
                if (vwMaster.View == null)
                    return null;
                if ((DataRowView)vwMaster.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMaster.View.CurrentItem).Row;
            }
        }
        #endregion 
        #endregion
        bool IsLoaded = false;
        string FART
        {
            get
            {

                return txtFART.Text;
            }
            set
            {
                txtFART.Text = value;
            }
        }
        BackgroundWorker bgWoker;
        DataRow dwData = null;
        public YCAFB02()
        {
            InitializeComponent();
            this.Name = "成衣库存";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
         
            m_Data = new YCAFB02Data(); 
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                bgWoker = new BackgroundWorker();
                bgWoker.DoWork += bgWoker_DoWork;
                bgWoker.RunWorkerCompleted += bgWoker_RunWorkerCompleted;                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        void bgWoker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("更新完成", StatusEventType.StopProgress);
        }

        void bgWoker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (dwData == null)
                return;
            EventDispatcher.Instance.RaiseStatus("开始更新", StatusEventType.StartProgress);
            m_Data.BuildData(dwData);
        }
        public void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            try
            { 

                DataRow dwData = SetData.FBST.NewRow();
                dwData["FBMANDANT"] = "00";
                dwData["FBART"] = FART;
                
                Result = m_Data.GetData(dwData,1);
                SetData.Clear();
                SetData.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            if (SetData.ICC_FB02.Rows.Count == 0)
            {
                MessageBox.Show("请先执行查询");
                btnQuery_Click(null, null);
                return;
            }
            string filter = "FBART LIKE '" +FART+ "%'";
            cvMaster.CustomFilter = filter;
        }
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = null;
        }
          
        
        public void btnPrintPreview_Click(object sender, EventArgs e)
        {
            PrnType = "OpenExcel";
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                DataSet setRpt = new DataSet();

                if (setRpt == null)
                    return;
                if (m_DataX == null)
                {
                    m_DataX = new YCAFB02Data(dicParamater);
                }
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                if (m_DataX.exAgent._app == null)
                {
                    m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt = new YCAFB02Report(dicParamater);
                m_rpt.dsSource = SetData;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.proxy = this.proxy;
                m_rpt.WriteExcelReport(PrnType);
            }
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            dwData = m_Data.INTEXDB.FBST.NewRow();
            dwData["FBMANDANT"] = "00";
            dwData["FBART"] = FART;
            bgWoker.RunWorkerAsync();
        } 
    }
}
